﻿//===============================================================================
// Microsoft Innovation Centre - Western Europe
// Copyright (c) 2008 Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using Microsoft.WVB.Framework;

namespace Microsoft.WVB.Services
{
    public static partial class EntityConverter
    {
        public static void FillAdContext(SyndicationFeed feed, AdContext context)
        {
            if (feed != null)
            {
                SyndicationItem item = feed.Items.First();
                if (item != null)
                {

                    XNamespace a = "http://www.w3.org/2005/Atom";
                    StringBuilder sb = new StringBuilder();
                    using (XmlWriter xw = XmlWriter.Create(sb))
                    {
                        item.Content.WriteTo(xw, "Content", "http://www.w3.org/2005/Atom");
                    }

                    XDocument xdoc = XDocument.Parse(sb.ToString(), LoadOptions.None);
                    XElement content = xdoc.Element(a + "Content");

                    context.CanSkip = Boolean.Parse(content.Element(a + "CanSkip").Value);
                    context.Click = item.Links.Where((sl) => (sl.RelationshipType == "AdClick")).First().Uri;
                    context.DisplayRegion = (AdOverlayMode)Enum.Parse(typeof(AdOverlayMode), content.Element(a + "AdOverlayMode").Value, true);
                    context.Duration = TimeSpan.FromSeconds(Double.Parse(content.Element(a + "Duration").Value));
                    context.EligibleContainer = (AdVisual)Enum.Parse(typeof(AdVisual), content.Element(a + "AdVisual").Value, true);
                    context.EndCallback = item.Links.Where((sl) => (sl.RelationshipType == "AdEndImp")).First().Uri;
                    context.Format = (AdFormat)Enum.Parse(typeof(AdFormat), content.Element(a + "AdFormat").Value, true);
                    context.ImpressionCallback = item.Links.Where((sl) => (sl.RelationshipType == "AdImp")).First().Uri;
                    context.MidCallback = item.Links.Where((sl) => (sl.RelationshipType == "AdMidImp")).First().Uri;
                    context.Source = item.Links.Where((sl) => (sl.RelationshipType == "self")).First().Uri;
                    context.ThrdEndCallback = item.Links.Where((sl) => (sl.RelationshipType == "AdThrdPartyEndImp")).First().Uri;
                    context.ThrdImpressionCallback = item.Links.Where((sl) => (sl.RelationshipType == "AdThrdPartyImp")).First().Uri;
                    context.ThrdMidCallback = item.Links.Where((sl) => (sl.RelationshipType == "AdThrdPartyMidImp")).First().Uri;
                }
            }
        }
    }
}
